package com.genius.dingding.util;

import java.util.List;

import com.dingtalk.api.DefaultDingTalkClient;
import com.dingtalk.api.request.OapiProcessinstanceCreateRequest;
import com.dingtalk.api.request.OapiProcessinstanceCreateRequest.ProcessInstanceApproverVo;
import com.dingtalk.api.response.OapiProcessinstanceCreateResponse;

/***
 * 封装审批方法
 * @author Administrator
 *
 */
public class ShenpiHelper {
	/**
     *  创建审批实例(新SDK)
     */
    public static String createShenpi(String accessToken,long agentId,String processCode,String userId,
    		long depId, String approvers, String cclist,String ccPosition,List<OapiProcessinstanceCreateRequest.FormComponentValueVo> formComponentValues) throws Exception {

    	DefaultDingTalkClient client = new DefaultDingTalkClient(GeniusConfig.OAPI_HOST+"/topapi/processinstance/create");
    	OapiProcessinstanceCreateRequest req = new OapiProcessinstanceCreateRequest();
    	req.setAgentId(agentId);
    	req.setProcessCode(processCode);
    	req.setOriginatorUserId(userId);
    	req.setDeptId(depId);
    	req.setApprovers(approvers);
    	req.setCcList(cclist);
    	req.setCcPosition(ccPosition);
    	req.setFormComponentValues(formComponentValues);
    	OapiProcessinstanceCreateResponse rsp = client.execute(req,accessToken);
//    	System.out.println(rsp.getBody());
        return rsp.getBody();
    }
    /**
     *  创建审批实例(新SDK)
     */
    public static String createEasyShenpi(String accessToken,String agentId,String processCode,String userId,
    		String depId, String approvers, String cclist,String ccPosition,List<OapiProcessinstanceCreateRequest.FormComponentValueVo> formComponentValues) throws Exception {

    	DefaultDingTalkClient client = new DefaultDingTalkClient(GeniusConfig.OAPI_HOST+"/topapi/processinstance/create");
    	OapiProcessinstanceCreateRequest req = new OapiProcessinstanceCreateRequest();
    	if(agentId != null && !agentId.equals("null") && !agentId.equals("")){
    		req.setAgentId(Long.parseLong(agentId));
    	}
    	req.setProcessCode(processCode);
    	req.setOriginatorUserId(userId);
    	
    	req.setDeptId(Long.parseLong(depId.split(",")[0]));
    	if(approvers != null && !approvers.equals("null") && !approvers.equals("")){
    		req.setApprovers(approvers);
    	}
    	if(cclist != null && !cclist.equals("null") && !cclist.equals("")){
    		req.setCcList(cclist);
    	}
    	if(ccPosition != null && !ccPosition.equals("null") && !ccPosition.equals("")){
    		req.setCcPosition(ccPosition);
    	}
    	req.setFormComponentValues(formComponentValues);
    	OapiProcessinstanceCreateResponse rsp = client.execute(req,accessToken);
//    	System.out.println(rsp.getBody());
        return rsp.getBody();
    }
    /**
     *  创建审批实例(新SDK)有会签或或签模式
     */
    public static String createDetailShenpi(String accessToken,Long agentId,String processCode,String userId,
    		String depId, List<ProcessInstanceApproverVo> approversV2, String cclist,String ccPosition,List<OapiProcessinstanceCreateRequest.FormComponentValueVo> formComponentValues) throws Exception {

    	DefaultDingTalkClient client = new DefaultDingTalkClient(GeniusConfig.OAPI_HOST+"/topapi/processinstance/create");
    	OapiProcessinstanceCreateRequest req = new OapiProcessinstanceCreateRequest();
    	if(agentId != null && !agentId.equals("null") && !agentId.equals("")){
    		req.setAgentId(agentId);
    	}
    	req.setProcessCode(processCode);
    	req.setOriginatorUserId(userId);
    	
    	req.setDeptId(Long.parseLong(depId.split(",")[0]));
    	if(approversV2 != null && approversV2.size() > 0){
//    		System.out.println("插入审批人");
//    		System.out.println(approversV2.size());
//    		for (int i = 0; i < approversV2.size(); i++) {
//				System.out.println(approversV2.get(i).getUserIds()+"---"+approversV2.get(i).getTaskActionType());
//			}
    		req.setApproversV2(approversV2);
    	}
    	if(cclist != null && !cclist.equals("null") && !cclist.equals("")){
    		req.setCcList(cclist);
    	}
    	if(ccPosition != null && !ccPosition.equals("null") && !ccPosition.equals("")){
    		req.setCcPosition(ccPosition);
    	}
    	req.setFormComponentValues(formComponentValues);
    	OapiProcessinstanceCreateResponse rsp = client.execute(req,accessToken);
//    	System.out.println(rsp.getBody());
        return rsp.getBody();
    }
}
